{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "91c116ff",
   "metadata": {},
   "source": [
    "-----\n",
    "* week09\n",
    "* 2023-11-17\n",
    "* python课程\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5bfbfed",
   "metadata": {},
   "source": [
    "# 1.元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d208126d",
   "metadata": {},
   "source": [
    "### 1.1认识元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "27649c56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699873705.6108494"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2b71aaf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.第一种创建方法\n",
    "my_tuple1 = 1699873705,100001,\"小明\", 18, '冰岛'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3280d5df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1699873705, 100001, '小明', 18, '冰岛')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(my_tuple1)\n",
    "print(type(my_tuple1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8271a71d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('jessa', 30, 45, 75, [89, 78])\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 2.第二种创建元组的方法\n",
    "my_tuple3 = tuple(('jessa', 30, 45, 75, [89,78]))\n",
    "print(my_tuple3)\n",
    "print(type(my_tuple3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7115eca4",
   "metadata": {},
   "source": [
    "### 1.2元组的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2259f3c0",
   "metadata": {},
   "source": [
    "* 1.关于序列数据的一般方法均适用于元组\n",
    "> 1. indexing and slicing(索引取值和切片)\n",
    "> 2. in方法查询【true表示元素在元组中】\n",
    "> 3. len()\n",
    "> 4. for循环遍历\n",
    "* 2.元组独有的一些常见方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "899d4a05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e8c2229a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(45, 75)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1b234639",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'jessa' in my_tuple3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fa543c4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'alex' in my_tuple3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6e41b6df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jessa\n",
      "30\n",
      "45\n",
      "75\n",
      "[89, 78]\n"
     ]
    }
   ],
   "source": [
    "for i in my_tuple3:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1638e9e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3.count(45) #count查询元素在里面出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1970d720",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3.index([89,78]) #index查询元素位于第几位"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a9a6ecd",
   "metadata": {},
   "source": [
    "-----\n",
    "### 列表[]元组()集合{}\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8de903d8",
   "metadata": {},
   "source": [
    "# 2.集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2cc7df16",
   "metadata": {},
   "outputs": [],
   "source": [
    "歌剧社 = {'小明','小红','李华','张三'}\n",
    "街舞社 = {'小明','李鹏','王芳'}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9ca43134",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明', '小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集：去除重复项\n",
    "歌剧社.union(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f5bae040",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明', '小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集的运算法则\n",
    "歌剧社 | 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ab439b5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集：找出重复项\n",
    "歌剧社.intersection(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "421072b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集的运算法则\n",
    "歌剧社 & 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "eeb269b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集：只存在一个里面的项\n",
    "歌剧社.difference(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3433d016",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集的运算法则\n",
    "歌剧社 - 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4c3c17b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'李鹏', '王芳'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "街舞社.difference(歌剧社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b8b38b91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对称差集：\n",
    "歌剧社.symmetric_difference(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3814b1fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(歌剧社.symmetric_difference)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "255eb9a0",
   "metadata": {},
   "source": [
    "# 3.字典（dict）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b6a45bf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu2201\":\"ziqi\",\n",
    "    \"stu2202\":\"xiaofeng\",\n",
    "    \"stu2203\":\"liuyu\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "919f216e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu2201': 'ziqi', 'stu2202': 'xiaofeng', 'stu2203': 'liuyu'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "64d63da8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'xiaofeng'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info[\"stu2202\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b333f4aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "info_01 = {\n",
    "    \"stu2201\":[\"ziqi\",2200001,18,\"女\"],\n",
    "    \"stu2202\":[\"xiaofeng\",2200002,0,\"男\"],\n",
    "    \"stu2203\":[\"liuyu\",2200003,19,\"\"],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "544540c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xiaofeng', 2200002, 0, '男']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info_01[\"stu2202\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34b34b0b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
